var filterHTMLTag = function (msg) {
    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    msg = msg.replace(/[|]*\n/, '') //去除行尾空格
    msg = msg.replace(/&npsp;/ig, ''); //去掉npsp
    return msg;
}

var mewchanJSON = JSON.parse(@.fs.readFile.sync(@.fs.realOSPath(__dirname,"..","conf","mewchan.json"),"text"));

var hakoJSONFile = @.fs.realOSPath(__dirname,"..","conf","hako.json");

if (@.fs.exists.file(hakoJSONFile)){
    mewchanJSON = @.merge.advanced({},mewchanJSON,JSON.parse(@.fs.readFile.sync(hakoJSONFile,"text")));
}

var jsonrpc = require(@.fs.realOSPath(__dirname,"..","kitties","jsonrpc","library","jsonrpc-client.js"));

module.exports={
    "deps": {},
    "build": [
        [
            "apidoc",
            "-i",
            "kitties/slime*",
            "-i",
            "kitties/lab",
            "-i",
            "kitties/wechat",
            "-o",
            "apidoc/",
            "--exclude-filters=\"node_modules\""
        ],
        ["mkdir" , "-p" , ".vscode/snippets"],
        function(){
            return @.async.resolve();
            var apiData = JSON.parse(@.fs.readFile.sync(@.fs.realOSPath(__dirname,"..","apidoc","api_data.json"),"text")).filter(function(api){
                return api.title;
            }).map(function(api){
                return {
                    url         : api.url,
                    type        : api.type,
                    title       : api.type == "jsonrpc" ? api.title.split("@").join(".") : api.title ,
                    description : filterHTMLTag(api.description)
                };
            });

            return @.async(function(){

                new jsonrpc({
                    rootURL : "http://127.0.0.1:" + mewchanJSON.playgrounds.http.port
                }).call("lab.inspect",[],this.test);

            }).then(function(){
                var labJSON = JSON.parse(@.fs.readFile.sync(@.fs.realOSPath(__dirname,"..","data","doc", (mewchanJSON.kitties.lab.name || "lab") + ".json"),"text"));
                
                var exported = {};

                this.pool.labJSON = labJSON;
               

                apiData.forEach(function(api) {
                    if (api.title[0] == "@") {
                        exported[api.url] = {
                            "prefix" : api.title.substr(1),
                            "body"   : [api.title.substr(1)],
                            "description" : api.description
                        }
                    } else {
                        ["lab","snapshot"].forEach(function(prefix){
                            exported[prefix + "@" + api.url] = {
                                "prefix" : prefix + "." + api.title,
                                "body"   : [prefix + "." + api.title],
                                "description" : api.description
                            };
                        })
                        
                    }
                    
                });

                @.keys(labJSON.errors).forEach(function(error){
                    exported["error@" + error] = {
                        "prefix" : error,
                        "body"   : [error],
                        "description" : labJSON.errors[error]
                    };

                    exported["lab.error@" + error] = {
                        "prefix" : "lab.errors." + error,
                        "body"   : ["lab.errors." + error],
                        "description" : labJSON.errors[error]
                    };
                    
                })
                

                @.keys(labJSON.models).forEach(function(table){
                    
                    exported[table] = {
                        "prefix" : table,
                        "body"   : [table],
                        "description" : labJSON.models[table]["!desc"]
                    };

                    @.keys(labJSON.models[table]).forEach(function(field){
                        exported[table + "." + field] = {
                            "prefix" : table + "." + field,
                            "body"   : [table + "." + field],
                            "description" : labJSON.models[table][field]["!desc"]
                        };  
                    })
                })

                @.fs.writeFile(@.fs.realOSPath(__dirname,"..",".vscode","snippets","javascript.json"), JSON.stringify(exported,null,4)).pipe(this);
            }).then(function(){

                var labJSON = this.pool.labJSON;
                var uiLocale = {};

                @.keys(labJSON.error_codes).forEach(function(code){
                    uiLocale[code] = labJSON.error_codes[code];
                });

                @.fs.writeFile(@.fs.realOSPath(__dirname,"..","ui","src","locale","error","zh-CN.js"), "export default " + JSON.stringify(uiLocale,null,4)).pipe(this);
                
            })
        }
    ],
    "dist": {}
}